<template>
  <div>
    <div class="main">
    <van-row>
      <van-col span="10">
        <h3>发表评论</h3>
      </van-col>
    </van-row>
    <hr border="2" />
    <!-- 留言板 -->
    <van-cell-group>
      <van-field
        v-model="message"
        rows="2"
        autosize
        type="textarea"
        maxlength="50"
        placeholder="请输入留言"
        show-word-limit
      />
    </van-cell-group>
    <van-button type="info" size='large' @click="addComment">提交留言</van-button>
    <van-list
        v-model="loading"
        :error.sync="error"
        error-text="点击加载更多"
        @load="onLoad"
        >
        <div v-for="(item,id) in list" :key="id">
           <div class="listbg">
               <span>第{{id+1}}楼</span>
               <span>用户：{{item.user_name}}</span>
               <span>发表时间：{{item.add_time.substr(0,10)}}</span>
               </div>
            <van-cell :title='item.content'/>
            </div>
        

        </van-list>
        </div>
  </div>
</template>
<script>
export default {
    data() {
        return {
            message:'',
            list: [],
            error: false,
            loading: false,
            age:1
        }
    },
    methods: {
       async onLoad() {
         const id=this.$route.query.userid
         const {data:res}= await this.$http.get(`/api/getcomments/${id}?pageindex=${this.age}`)
         console.log(res)
         this.list=this.list.concat(res.message)
         this.age++
         this.error = true
         this.loading=false
    },
    async onLoad2() {
         const {data:res}= await this.$http.get(`/api/getcomments/87?pageindex=1`)
         this.list=res.message
         this.error = true
         this.loading=false
    },
   async addComment(){
       const {data:res}=await this.$http.post(`/api/postcomment/87`,{content:this.message})
       if(res.status!=0) return this.$notify({ type: 'error', message: '添加失败' })
        this.$notify({ type: 'warning', message: '添加成功' })
        this.onLoad2()
    }
    },
}
</script>
<style lang="less" scoped>
.van-list .listbg {
    height: 30px;
    background-color: #ccc;
    font-size: 12px;
    line-height: 30px;
    text-align: left;
}
.main {
    margin-bottom: 50px;
}
</style>
